package cn.kgc.ubagnet.controller;

import cn.kgc.ubagnet.pojo.Cart;
import cn.kgc.ubagnet.pojo.CartPro;
import cn.kgc.ubagnet.service.cart.CartService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {
    @Resource
    CartService cartService;

    @GetMapping("/cart/list")
    @ResponseBody
    public List<CartPro> cartprolist(){
        List<CartPro> list=cartService.CarttProList(1);
        if(list.size()>0){
            return list;
        }else{
            return null;
        }
    }
    @PostMapping("/cart/add")
    @ResponseBody
    public Cart AddCart(@RequestBody Cart cart, HttpSession session){
        String chars = "0123456789";
        char[] rands = new char[6];
        for (int i = 0; i < 6; i++) {
            int rand = (int) (Math.random() * 10);
            rands[i] = chars.charAt(rand);
        }
        cart.setCartid(rands.toString());
        cart.setUserid(1);
        cart.setTotalprice(cart.getPrice()*cart.getNum());
        int result=cartService.AddCart(cart);
        if(result>0) return cart;
        else return null;
    }
}
